<?php
defined("_JEXEC") or die("Restricted access");

/**
 * JoomlaIniTranslator Table class
 */
class TableJoomlaIniTranslator extends JTable
{
	const JIT_STATUS_NEVER = "never";
	const JIT_STATUS_NEW = "new";
	const JIT_STATUS_DONE = "done";
	
	/**
	 * Primary Key
	 *
	 * @var int
	 */
	var $id = null;

	/**
	 * @var string
	 */
	var $srcLangCode = null;
	
	/**
	 * @var string
	 */
	var $srcLangDir = null;

	/**
	 * @var string
	 */
	var $tarLangCode = null;
	
	/**
	 * @var string
	 */
	var $tarLangDir = null;
	
	/**
	 * @var string
	 */
	var $langDir = null;

	/**
	 * @var enum
	 */
	var $status = null;

	/**
	 * @var integer
	 */
	var $created = null;

	function __construct(& $db) {
		parent::__construct("#__joomlainitranslator", "id", $db);
	}
	
	function check() {
		// check lang code
		$pattern = "/^[a-z]{2}-[A-Z]{2}$|^[a-z]{2}$/";
		$n1 = preg_match($pattern, $this->srcLangCode);
		$n2 = preg_match($pattern, $this->tarLangCode);
		//var_dump($this->srcLangCode, $this->tarLangCode, $n1, $n2); die;
		if ($n1 <= 0 ||	$n2 <= 0 ||	strlen($this->langDir) <= 0) {
			$this->setError(JText::_('TABLE_INVALID_CODEFORMAT'));
			return false;
		}
		
		// check lang dir
		if (!(file_exists($this->langDir)) || !(is_dir($this->langDir))) {
			$this->setError(JText::_('TABLE_INVALID_LANGDIR'));
			return false;
		}
		
		$it = new DirectoryIterator($this->langDir);
		$flag = 0;
		foreach ($it as $i) {
			if ($i->isDir() && $i->getFilename() == $this->srcLangDir && $i->isReadable()) {
				$flag++;
			}
			if ($i->isDir() && $i->getFilename() == $this->tarLangDir && $i->isWritable()) {
				$flag++;
			}
		}
		
		if ($flag != 2) {
			$this->setError(JText::_('TABLE_DIR_MUST_EXISTED_AND_HAVE_PERMISSION'));
			return false;
		}
		
		return true;
	}
}
?>